
public class LibraryService {
    // 借书逻辑
    public boolean borrowBook(User user, Book book) {
        // 条件判断：
        if (book.getStatus() == BookStatus.AVAILABLE && book.getStock() > 0 && user.getBorrowedBooks().size() < 3) {
            book.setStock(book.getStock() - 1);
            if (book.getStock() == 0) {
                book.setStatus(BookStatus.BORROWED);
            }
            user.getBorrowedBooks().add(book);
            return true;
        }
        return false;
    }

    // 还书逻辑（简单版罚款：超期1天罚款1元）
    public double returnBook(User user, Book book, int overdueDays) {
        if (user.getBorrowedBooks().contains(book)) {
            user.getBorrowedBooks().remove(book);
            book.setStock(book.getStock() + 1);
            book.setStatus(BookStatus.AVAILABLE);
            return Math.max(0, overdueDays * 1.0); // 返回罚款金额
        }
        return 0.0;
    }

    public void borrowBookWithException(User user, Book book) {
        // 检查借阅上限
        if(user.getBorrowedBooks().size() >= 3) {
            throw new LibraryException("已达到借阅上限");
        }

        try{
            book.borrow();
            user.borrowBook(book);
        } catch (IllegalStateException e) {
            throw new LibraryException(e.getMessage());
        }

        // 其他业务逻辑（检查库存、更新状态等）
        if(book.getStock() <= 0) {
            throw new LibraryException("库存不足");
        }
        // ... 剩余借书逻辑 ...
        if (book.getStatus() != BookStatus.AVAILABLE) {
            throw new LibraryException("BOOK_UNAVAILABLE");
        }
        if (book.getStock() <= 0) {
            throw new LibraryException("NO_STOCK");
        }
        if (user.getBorrowedBooks().size() >= 3) {
            throw new LibraryException("LIMIT_EXCEEDED");
        }
    }
}